import { createStore, applyMiddleware } from 'redux'
import languageReducer from './language/languageReducer'
import recommendProductsReducer from './recommendProducts/recommendProductsReducer'
import thunk from 'redux-thunk' 
// 中间件
import { actionLog } from './middleware/actionLog'
// slice
import { productDetailSlice } from './productDetail/slice'
import { combineReducers, configureStore } from '@reduxjs/toolkit'
import { productSearchSlice } from './productSearch/slice'
import { userSlice } from './user/slice'
import { shoppingCartSlice } from './shoppingCart/slice'
import { orderSlice } from './order/slice'


// 使用combineReducers将两个reducer合并
const rootReducer = combineReducers({
  language: languageReducer,
  recommendProducts: recommendProductsReducer,
  productDetail: productDetailSlice.reducer,
  productSearch: productSearchSlice.reducer,
  user: userSlice.reducer,
  shoppingCart: shoppingCartSlice.reducer,
  order: orderSlice.reducer
})

// applyMiddleware 存储增强器,用来包裹中间件
// const store = createStore(rootReducer, applyMiddleware(thunk, actionLog))
// 使用configureStore代替原生createStore
const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) => [...getDefaultMiddleware(), actionLog],
  devTools: true
})


// ReturnType条件类型:获取函数返回值类型,可获得state数据的类型
// 类型的定义使用type关键字声明
export type RootState = ReturnType<typeof store.getState>

export default store